package com.sample.BouncingBall;

import java.util.Random;




import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.KeyEvent;
//import android.view.MotionEvent;
//import android.view.View.OnTouchListener;
import android.view.View;
import android.widget.TextView;

public class BouncingView extends View /*implements OnTouchListener*/{
	private Random ran=new Random();
	private Ball ball1;
	private Paint paint1;
	private Platform platform1=new Platform();
	private Platform platform2=new Platform();
	private Rect rect, rect2;
	private TextView text;	
	private boolean isVisible=false;
	private boolean hasCollided=false;
	private boolean aiHasCollided=false;
	private int score=0;
	private int aiScore=0;
	private int speed=15;
	private int color=Color.BLUE;
	private int momentum=0;
	
	public BouncingView(Context context) {
		super(context);
		setFocusable(true);
		ball1= new Ball(/*context, R.drawable.ball*/);
		speed=ball1.getxVel();
		setFocusableInTouchMode(true);
		//this.setOnTouchListener(this);
	}
    public BouncingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFocusable(true);
		ball1= new Ball(/*context, R.drawable.ball*/);
		setFocusableInTouchMode(true);
        }
@Override
public void onDraw(Canvas c){
int X2=platform1.getX()+platform1.getLength();
int X3=platform2.getX()+platform2.getLength();
rect=new Rect(platform1.getX(), 660, X2, 675);	
rect2=new Rect(platform2.getX(), 0, X3, 15);
if (speed>30){
speed=30;	
}
ball1.move(speed, momentum);	
paint1= new Paint();
paint1.setColor(color);
paint1.setAntiAlias(true);
update();
c.drawCircle(ball1.getX(), ball1.getY(), 20, paint1);
c.drawRect(rect, paint1);
c.drawRect(rect2, paint1);
//c.drawBitmap(ball1.getImage(), ball1.getX(), ball1.getY(), null);
invalidate();
}
/*public boolean onTouch(View v, MotionEvent event) {
	ball1.setX((int)event.getX());
	ball1.setY((int)event.getY());
	return true;
}*/
public boolean onKeyDown(int keyCode, KeyEvent msg) {
	  if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && platform1.getX()>= 0) {
		  int temp=-(speed+15);
		  platform1.move(temp); 
		  if (platform1.getDirection()<=0){
			  int tempDirect=platform1.getDirection()-1;
			  platform1.setDirection(tempDirect);
		  }
		  else{
			platform1.setDirection(0);	  
		  }
      }

      if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && platform1.getX()<= 390) {
    	  int temp=speed+15;
    	  platform1.move(temp); 
    	  if (platform1.getDirection()>=0){
    		  int tempDirect=platform1.getDirection()+1;
    		  platform1.setDirection(tempDirect);
    		  }
    		  else{
    		platform1.setDirection(0);	
          }
      }
      if (keyCode == KeyEvent.KEYCODE_SPACE && isVisible == true) {
    	  isVisible=false;
    	  reset();
          }

      return true;
      }
private void update(){
platform2.move(platform2.think(platform2, ball1));	
collisionCheck();	
aiCollisionCheck();
String scores= Integer.toString(score) + ":" + Integer.toString(aiScore);
if (ball1.getY()>680){	
if (isVisible==false){
aiScore++;	
}	
isVisible=true;
ball1.setY(100000);
}
if (ball1.getY()<0){
if (isVisible==false){
score++;	
}	
isVisible=true;
ball1.setY(100000);	
}
text.setText(scores);
if (ball1.getY()<300){
hasCollided=false;
}
if (ball1.getY()>450){
aiHasCollided=false;
}
}
private void collisionCheck(){
int X2=platform1.getX()+platform1.getLength();
if (platform1.getX()<ball1.getX() && ball1.getX()<X2 && ball1.getY()>630 && hasCollided == false){
hasCollided=true;
ball1.setYDirection(true);
speed++;
if(ball1.getBallDirection()==false && platform1.getDirection()>0){
momentum+=platform1.getDirection();	
}
else if(ball1.getBallDirection()==true && platform1.getDirection()<0){
momentum+=platform1.getDirection();		
}
else if(ball1.getBallDirection()==true && platform1.getDirection()>0){
momentum-=platform1.getDirection();		
}
else if(ball1.getBallDirection()==false && platform1.getDirection()<0){
momentum-=platform1.getDirection();		
}
if(ball1.getX()<platform1.getX()+25 && ball1.getBallDirection()==false){
ball1.setXDirection(true);	
speed+=2;
momentum+=2;
} 
if(ball1.getX()>X2-25 && ball1.getBallDirection()==true){
ball1.setXDirection(false);	
speed+=2;
momentum+=2;
} 
if (ran.nextInt(10)<6){
color=changecolor();	
}
}
}
private void aiCollisionCheck(){
int X2=platform2.getX()+platform2.getLength();
if (platform2.getX()<ball1.getX() && ball1.getX()<X2 && ball1.getY()<40 && aiHasCollided == false){
aiHasCollided=true;
ball1.setYDirection(false);
speed++;
if(ball1.getBallDirection()==false && platform2.getDirection()>0){
momentum+=platform2.getDirection();	
}
else if(ball1.getBallDirection()==true && platform2.getDirection()<0){
momentum+=platform2.getDirection();		
}
else if(ball1.getBallDirection()==true && platform2.getDirection()>0){
momentum-=platform2.getDirection();		
}
else if(ball1.getBallDirection()==false && platform2.getDirection()<0){
momentum-=platform2.getDirection();		
}
if (ran.nextInt(10)<6){
color=changecolor();	
}
}	
}
public void setTextView(TextView tv){
text=tv;	
text.setVisibility(View.VISIBLE);

}

private void reset(){
ball1.setX(ran.nextInt(400));
ball1.setY(50);
ball1.setyVel(15);
platform1.setX(200);
platform2.setX(200);
ball1.setYDirection(false);
speed=15;
momentum=0;
}
private int changecolor(){
int color=ran.nextInt(5);
if (color==0){
color=Color.BLUE;	
}
else if (color==2){
color=Color.RED;	
}
else if (color==3){
color=Color.GREEN;	
}
else if (color==4){
color=Color.WHITE;	
}
else{
color=Color.YELLOW;	
}
return color;	
}
public int getMomentum(){
return momentum;	
}
}
